local skel = fk.CreateSkill {
  name = "emo__zhuguo",
}

Fk:loadTranslationTable{
  ["emo__zhuguo"] = "柱国",
  [":emo__zhuguo"] = "你可以横置以将一张牌当【出其不意】/【无懈可击】使用，然后你获得并明置展示牌/抵消牌。",

  ["#emo__zhuguo"] = "柱国:横置以将一张牌当【%arg】使用",
}

local DIY = Pcall(require, "packages/diy_utility/diy_utility")

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "unexpectation,nullification",
  prompt = function (self, player)
    if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):matchExp("nullification") then
      return "#emo__zhuguo:::nullification"
    end
    return "#emo__zhuguo:::unexpectation"
  end,
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local name = "unexpectation"
    if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):matchExp("nullification") then
      name = "nullification"
    end
    local c = Fk:cloneCard(name)
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  before_use = function(self, player, use)
    use.extra_data = use.extra_data or {}
    use.extra_data[skel.name] = player
    player:setChainState(true)
  end,
  enabled_at_play = function(self, player)
    return not player.chained
  end,
  enabled_at_response = function (self, player, response)
    return not response and not player.chained
  end,
})

skel:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return data.extra_data and data.extra_data[skel.name] == player and not player.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = {}
    if data.card.trueName == "nullification" then
      local e = data.responseToEvent
      if e and e.card then
        cards = Card:getIdList(e.card)
      end
    elseif data.extra_data.emo__zhuguo_shown then
      cards = data.extra_data.emo__zhuguo_shown
    end
    cards = table.filter(cards, function (id)
      return room:getCardOwner(id) ~= player
    end)
    if #cards > 0 then
      room:obtainCard(player, cards, true, fk.ReasonPrey, player, skel.name)
      cards = table.filter(cards, function (id)
        return table.contains(player.player_cards[Player.Hand], id)
      end)
      DIY.showCards(player, cards)
    end
  end,
})

skel:addEffect(fk.CardShown, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    local current = player.room.logic:getCurrentEvent()
    if current.parent and current.parent and current.parent.event == GameEvent.CardEffect then
      local e = current.parent
      local effect = e.data
      if not table.contains(effect.card.skillNames, skel.name) then
        return
      end
      effect.extra_data = effect.extra_data or {}
      effect.extra_data.emo__zhuguo_shown = effect.extra_data.emo__zhuguo_shown or {}
      table.insertTableIfNeed(effect.extra_data.emo__zhuguo_shown, data.cardIds)
    end
  end,
})

return skel
